package plate

import "unicode"

func theFirstIsHan(str string, dafault string) (remain string, res string) {
	if len(str) < 3 {
		remain = str
		res = dafault
		return
	} else {
		count := 0
		for _, v := range str[0:3] {
			if unicode.Is(unicode.Han, v) {
				count++
			}
		}
		if count == 1 {
			remain = str[3:]
			res = str[0:3]
			return
		} else {
			remain = str
			res = dafault
			return
		}
	}
}

func theSecondIsEnglish(str string, dafault string) (remain string, res string) {
	if len(str) >= 1 {
		if unicode.IsLetter(int32(str[0])) {
			res = str[0:1]
			remain = str[1:]
		} else {
			//fmt.Println("theSecondIsEnglish dafault")
			remain = str
			res = dafault
		}
	} else {
		res = dafault
	}

	return
}

func theFirstIsHanTrue(str string) (remain string, res string, ret bool) {
	if len(str) < 3 {
		remain = str
		return
	} else {
		count := 0
		for _, v := range str[0:3] {
			if unicode.Is(unicode.Han, v) {
				count++
			}
		}
		if count == 1 {
			remain = str[3:]
			res = str[0:3]
			ret = true
			return
		} else {
			remain = str
			return
		}
	}
}

func theLastIsHan(str string) (remain string, res string, ret bool) {
	if len(str) < 3 {
		remain = str
		ret = false
		return
	} else {
		count := 0
		for _, v := range str[len(str)-3:] {
			if unicode.Is(unicode.Han, v) {
				count++
			}
		}
		if count == 1 {
			remain = str[0 : len(str)-3]
			res = str[len(str)-3:]
			ret = true
			return
		} else {
			remain = str
			return
		}
	}

}

func theLastIsNum(str string, num int, dafault string) (remain string, res string) {
	if len(str) >= num {
		res = str[0:num]
		if len(str) > num {
			remain = str[num:]
		}
	} else {
		res = str
		for i := 0; i < num-len(str); i++ {
			res = res + dafault
		}
	}

	return
}

func theFirstIsTwoHan(str string) (remain string, res string, ret bool) {
	if len(str) < 6 {
		remain = str
		return
	} else {
		count := 0
		for _, v := range str[0:6] {
			if unicode.Is(unicode.Han, v) {
				count++
			}
		}
		if count == 2 {
			remain = str[6:]
			res = str[0:6]
			ret = true
			return
		} else {
			remain = str
			return
		}
	}
}

func blueMakeUp(initial string) (plateNo string) {
	var remain string
	var temp string
	remain, temp = theFirstIsHan(initial, handler.defProvince)
	plateNo = temp
	remain, temp = theSecondIsEnglish(remain, handler.defCity)
	plateNo = plateNo + temp
	_, temp = theLastIsNum(remain, 5, "1")
	plateNo = plateNo + temp
	return
}

func greenMakeUp(initial string) (plateNo string) {
	var temp, temp2 string
	var remain string
	var ret bool
	remain, temp, ret = theFirstIsTwoHan(initial)
	if ret {
		plateNo = temp
		_, temp = theLastIsNum(remain, 5, "1")
		plateNo = plateNo + temp
	} else {
		remain, temp = theFirstIsHan(initial, handler.defProvince)
		plateNo = temp
		remain, temp2, ret = theLastIsHan(remain)
		if ret {
			_, temp = theLastIsNum(remain, 6, "1")
			plateNo = plateNo + temp
			plateNo = plateNo + temp2
		} else {
			_, temp = theLastIsNum(remain, 7, "1")
			plateNo = plateNo + temp
		}
	}
	return
}

func yellowMakeUp(initial string) (plateNo string) {
	var temp, temp2 string
	var remain string
	var ret bool
	remain, temp = theFirstIsHan(initial, handler.defProvince)
	plateNo = temp
	remain, temp2, ret = theLastIsHan(remain)
	if ret {
		_, temp = theLastIsNum(remain, 5, "1")
		plateNo = plateNo + temp
		plateNo = plateNo + temp2
	} else {
		_, temp = theLastIsNum(remain, 6, "1")
		plateNo = plateNo + temp
	}
	return
}

func blackMakeUp(initial string) (plateNo string, color string) {
	var temp, temp2 string
	var remain string
	var ret bool
	color = "黑"
	remain, temp2, ret = theLastIsHan(initial)
	if ret {
		remain, temp, ret = theFirstIsHanTrue(remain)
		if ret {
			plateNo = temp
			_, temp = theLastIsNum(remain, 5, "1")
			plateNo = plateNo + temp
			plateNo = plateNo + temp2
		} else {
			_, temp = theLastIsNum(remain, 6, "1")
			plateNo = plateNo + temp
			plateNo = plateNo + temp2
		}
	} else {
		color = "蓝"
		plateNo = blueMakeUp(initial)
	}
	return
}

func theFirstIsHanDe(str string) (ret bool) {
	if len(str) < 3 {
		return
	} else {
		count := 0
		for _, v := range str[0:3] {
			if unicode.Is(unicode.Han, v) {
				count++
			}
		}
		if count == 1 {

			ret = true
			return
		} else {
			return
		}
	}
}
